package com.guuidea.bio;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {

    public static void main(String[] args) throws Exception {

        //1. 创建ServerSocket 对象

        ServerSocket ss = new ServerSocket(9999);


        while (true) {
            //2 监听客户端

            Socket s = ss.accept();   //阻塞

            //3 从连接中取出输入流来接收消息
            InputStream is = s.getInputStream();

            byte[] b = new byte[10];
            is.read(b);

            String clientIP = s.getInetAddress().getHostAddress();
            //4 从连接中取出输出流并回话
            System.out.println(clientIP + "说:" + new String(b).trim());

            OutputStream os = s.getOutputStream();
            os.write("没钱".getBytes());
            // 5关闭
            s.close();
        }


    }
}
